package sword.chapter4LinkList;

import leetcode.t4LinkedList.ListNode;

/**
 * 反转链表
 * 1. 先记录cur下一个值
 * 2. 从左到右赋值
 *
 * 实际上是双指针，P1=pre P2=cur，每次将cur.next指向pre，完成局部翻转。然后pre cur都向前移动一步。
 *
 * @author K
 * @date 2021/11/26 09:01
 */
public class S24ReverseList {
    public static void main(String[] args) {
        ListNode nodeA = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(3);
        ListNode node4 = new ListNode(4);
        ListNode node5 = new ListNode(5);
        ListNode node6 = new ListNode(6);
        nodeA.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        node5.next = node6;
        ListNode result = s24(nodeA);
        System.out.println();
    }

    public static ListNode s24(ListNode head) {
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }
}
